# Maintainer: Martell Malone <martellmalone@gmail.com>
# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=git
# Marat Radchenko is trying to upstream mingw-w64 patches
# and Johannes Schindelin made a branch of msysgit for it
# Set _based_on_dscho_w64_msysgit to yes to build that.
# Patches that required re-basing for this repository are
# infixed with "-w64-msysgit".
_based_on_dscho_w64_msysgit=no
if [ "${_based_on_dscho_w64_msysgit}" = "yes" ]; then
  SRC_DIR=${_realname}-dscho
  GIT_URL="${SRC_DIR}::git+https://github.com/dscho/git.git#branch=w64-msysgit"
else
  SRC_DIR=${_realname}
  GIT_URL="${SRC_DIR}::git+https://github.com/msysgit/git.git"
fi

pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_ver_base=2.0.0
pkgver=2.0.0.47118.8768113
pkgrel=1
pkgdesc="The fast distributed version control system (mingw-w64)"
arch=('any')
url="https://git-scm.com/"
license=('GPL2')

depends=("${MINGW_PACKAGE_PREFIX}-libiconv")
makedepends=('git')

options=(!libtool strip staticlibs)

source=(${GIT_URL}
        "0000-Add-SEARCH_LIBS-to-GIT_CHECK_FUNC.patch"
        "0001-Don-t-redefine-_ReadWriteBarrier-in-malloc.c.h.patch"
        "0002-Undefine-FORCEINLINE-on-MinGW-w64-in-malloc.c.h.patch"
        "0003-Fix-BASIC_LDFLAGS-and-COMPAT_CFLAGS-for-64bit-MinGW-.patch"
        "0004-Also-look-in-winsock2.h-for-sockaddr_storage-and-loo.patch"
        "0005-Don-t-redefine-some-things-MinGW-w64-provides.patch"
        "0006-Include-ntdef.h-on-MinGW-w64-for-REPARSE_DATA_BUFFER.patch"
        "0007-Include-winsock2.h-and-not-netdb.h-on-MinGW-w64.patch"
        "0008-fix-mingw64-compat-wide-char.patch"
        "0009-Change-inline-definition-of-fork-for-mingw64.patch"
        "0010-Use-GIT_CHECK_FUNC-for-socket-and-basename.patch"
        "0010-w64-msysgit-Use-GIT_CHECK_FUNC-for-socket-and-basename.patch"
        "0011-Use-GIT_CHECK_FUNC-for-regcomp-regex.patch"
        "0011-w64-msysgit-Use-GIT_CHECK_FUNC-for-regcomp-regex.patch"
        "0012-Use-system-dirent.h-on-MinGW-w64.patch"
        "0013-Make-pthread-wrapper-only-for-mingw.patch"
        "0014-Do-not-compile-dirent-on-mingw64.patch"
        "0015-Alter-fscache-for-mingw64.patch"
        "0016-Use-time.h-versions-of-gmtime-localtime-_r.patch")

sha256sums=('SKIP'
            'bc8648a478137957a4c421dc97355ef75a930e549836a4230a3ce26f80cafdcb'
            '7cd9b9f822a3c4b5ebf088af1d01ceff918c5e74a0590decec6d9ef3a8c5d0cc'
            '19b39c27dd007e2d73ef623f3d9db910bffccac00c30c1df3be9922d9ce8d718'
            'cd778d56ad03f863fa230072299b36f0b1c2e49c7914fa65a3fcb8d7bd8f0e07'
            'ef3021be142259cdecde7f9632a10102b0f7d570eb6e16b7da45050775b02a35'
            'c48654b94c1369c3f939689634dfb95382a4b9464f699d9e8afd53dc8ded625d'
            '331e576532924e6a5ae99a7b87e5ee0a3c4687ccc209e2eaee710621152dd2de'
            '4b9216db5dce6803174075148fa2b04728c06865a687e258bd134a212533c52e'
            '82c141851bca5b5b1695204a129827c99ca05d574c89057ec035fbfd42c141f4'
            '13de3242d88dd595f6bf5597515b118049f7b0b59a445feddcbbf34b3165037d'
            '5f27d68f07a39acb3c85e886f6bc5d090d67f2907a20912e7050f0040c8e1fe0'
            '3cfd9e1df08475a1858eaacacbdc470de14bf88aed59afc229b977057329b96b'
            '1e8d4cc26b8c90924d36ddc5638208ddbf95e7c8adcc29afdc6356214a941326'
            'ade0009f7545eddf793cc1af7e6850ded32680b9a87606365a242a970e8e6f89'
            'e012a121c3d5b196b18a44b2ba36dc3cffe3a28c9f8bff5470ad07ea1499cb46'
            'eed3357b4b459d113c524158156f759916d895373ff42c826c137ca8784433fa'
            '61eb657cf8821cf2b372000c60a1fb616375f4b55e3d5b85c0ce5e03d04e49da'
            'df1aa1a5a5b1ef512f999891ce855e617d376e60839925eafa82b49f4883ff87'
            'dc97b4ec2263e6b5142e339e3e317dc1f10b9265ed5175bc8e026df509debdb0')

pkgver() {
  cd "${srcdir}"/${SRC_DIR}
  printf "%s.%s.%s" "$_ver_base" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  export PYTHON_PATH=/usr/bin/python2
  cd "${srcdir}"/${SRC_DIR}

  git am "${srcdir}"/0000-Add-SEARCH_LIBS-to-GIT_CHECK_FUNC.patch
  if [ "${_based_on_dscho_w64_msysgit}" = "no" ]; then
    git am "${srcdir}"/0001-Don-t-redefine-_ReadWriteBarrier-in-malloc.c.h.patch
    git am "${srcdir}"/0002-Undefine-FORCEINLINE-on-MinGW-w64-in-malloc.c.h.patch
    git am "${srcdir}"/0003-Fix-BASIC_LDFLAGS-and-COMPAT_CFLAGS-for-64bit-MinGW-.patch
  fi
  git am "${srcdir}"/0004-Also-look-in-winsock2.h-for-sockaddr_storage-and-loo.patch
  if [ "${_based_on_dscho_w64_msysgit}" = "no" ]; then
    git am "${srcdir}"/0005-Don-t-redefine-some-things-MinGW-w64-provides.patch
    git am "${srcdir}"/0006-Include-ntdef.h-on-MinGW-w64-for-REPARSE_DATA_BUFFER.patch
  fi
  git am "${srcdir}"/0007-Include-winsock2.h-and-not-netdb.h-on-MinGW-w64.patch
  if [ "${_based_on_dscho_w64_msysgit}" = "no" ]; then
    git am "${srcdir}"/0008-fix-mingw64-compat-wide-char.patch
    git am "${srcdir}"/0009-Change-inline-definition-of-fork-for-mingw64.patch
  fi
  if [ "${_based_on_dscho_w64_msysgit}" = "no" ]; then
    git am "${srcdir}"/0010-Use-GIT_CHECK_FUNC-for-socket-and-basename.patch
    git am "${srcdir}"/0011-Use-GIT_CHECK_FUNC-for-regcomp-regex.patch
  else
    git am "${srcdir}"/0010-w64-msysgit-Use-GIT_CHECK_FUNC-for-socket-and-basename.patch
    git am "${srcdir}"/0011-w64-msysgit-Use-GIT_CHECK_FUNC-for-regcomp-regex.patch
  fi
  if [ "${_based_on_dscho_w64_msysgit}" = "no" ]; then
    git am "${srcdir}"/0012-Use-system-dirent.h-on-MinGW-w64.patch
    git am "${srcdir}"/0013-Make-pthread-wrapper-only-for-mingw.patch
    git am "${srcdir}"/0014-Do-not-compile-dirent-on-mingw64.patch
    git am "${srcdir}"/0015-Alter-fscache-for-mingw64.patch
  fi
  git am "${srcdir}"/0016-Use-time.h-versions-of-gmtime-localtime-_r.patch
  autoreconf -fi
}

build() {

  export PYTHON_PATH=/usr/bin/python2
  cd "$srcdir"

  [ -d build-${CARCH} ] && rm -rf build-${CARCH}
  cp -rf ${SRC_DIR} build-${CARCH}
  cd build-${CARCH}

  ./configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --libexecdir=${MINGW_PREFIX}/lib \
    --with-editor=vim \
    --htmldir=${MINGW_PREFIX}/share/doc/git/html \
    --mandir=${MINGW_PREFIX}/share/man \
    --with-curl=${MINGW_PREFIX}

  make INSTALLDIRS=vendor -j1 all V=1
  make -C contrib/subtree prefix=/usr all V=1
}

package() {
  export PYTHON_PATH=/usr/bin/python2
  cd "$srcdir"/build-${CARCH}
  make INSTALLDIRS=vendor DESTDIR="$pkgdir" install
}
